package com.maiworld.api.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.maiworld.api.dto.StudentDTO;
import com.maiworld.api.vo.ResultVO;
import com.maiworld.seller.service.StudentService;

import uitl.HttpUtil;
import wxutils.Constant;



@RestController
@RequestMapping("/student")
public class StudentController {

	@Reference
	private StudentService studentService;

	@RequestMapping("/findOne/{openid}")
	public ResultVO<StudentDTO> findOne(@PathVariable("openid") String openid){
		return studentService.findOne(openid);
	}
	//上传驾驶证照片    (测试通过)
	@RequestMapping(value = "addStudentUrl", method=RequestMethod.POST)
	public Map<String, Object> addStudentUrl(
			@RequestBody Map<String,String> params
			){
		return  studentService.addStudentUrl(params);
	}
	//获取驾照图片(测试通过)
	@RequestMapping("findStu")
	public Map<String, Object> findStu(Long sid){
		return  studentService.findOneSid(sid);
	} 
	
	//修改学原信息 (测试通过)
	@RequestMapping(value="update",method=RequestMethod.POST)
	public Map<String, Object> updateStudent(
			@RequestBody Map<String,String> params
			){
		return  studentService.updateStudent(params);
	}
	//学员登录    (测试通过)
	@RequestMapping("/login")
	public Map<String, Object> login(String code,String nickname, String head_pic) {
		Map<String, Object> result = new HashMap<String,Object>();

		try {
			nickname = new String((nickname).trim().getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
		}
    	   	
		String url ="https://api.weixin.qq.com/sns/jscode2session" + "?appid=" +Constant.appid+ "&secret=" +Constant.appsecret+ "&js_code=" + code + "&grant_type="  
                +"authorization_code";  
        // 发送请求  
        String data = HttpUtil.get(url);
        //数据处理
        JSONObject jsonobjectF = JSONObject.parseObject(data);
        String sessionKey = jsonobjectF.getString("session_key");
        String openid = jsonobjectF.getString("openid");
        
       //System.out.print(q);

        result = studentService.login(openid, nickname, head_pic);
        result.put("session_key", sessionKey);
        
        return result;

	} 
	
	//学员选择驾校自己的分校      (测试通过)
	@RequestMapping("/updateStatus")
	public Map<String, Object> updateStatus(Long sid, String openid, String status,String session_key){
		return studentService.updateStatus(sid, openid, status);
		
	} 
	//报名成功，改变状态 (测试通过)
	@RequestMapping("/updatOrderNews")
	public Map<String, Object> updatOrderNews(Long order_id,String session_key){
		return studentService.updatOrderNews(order_id);
		
	} 
}
